package main

import (
	"cn.hughie/learngo/func/fibo"
	"io"
	"fmt"
	"strings"
	"bufio"
	"os"
)

type intGen func() int

/*func (deph Deph) Read(p []byte) (n int, err error) {
	i := deph()
	if i > 10000 {
		return 0, io.EOF
	}
	s := fmt.Sprintf("%d\n", i)
	return strings.NewReader(s).Read(p)
}*/

func (g intGen) Read(
	p []byte) (n int, err error) {
	next := g()
	if next > 10000 {
		return 0, io.EOF
	}
	s := fmt.Sprintf("%d\n", next)

	// TODO: incorrect if p is too small!
	return strings.NewReader(s).Read(p)
}

func readFonacciAsFile(r io.Reader, filename string) {
	file, e := os.Create(filename)
	if (e != nil) {
		panic(e)
	}
	defer file.Close()
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		file.WriteString(scanner.Text())
		file.WriteString("\n")
	}
}

func main() {
	//adder := adder.Add()
	//for i := 0; i <= 10; i++ {
	//	fmt.Println(adder(i))
	//}
	//todo 别名实现接口需要标记类型 结构体实现接口则不需要
	var fibonacci intGen = fibo.Fibonacci()
	readFonacciAsFile(fibonacci, "fib.txt")

}
